/* ============================================================================= */
/*        MATCHING PROCEDURE: ACCEPTED VS. REJECTED FIRMS            */
/* ============================================================================= */

clear all

/* Start logging */
capture log close
log using "$logs\regression_multiple_confs_rejected.log", replace

/* ============================================================================= */
/*        STEP 1: IDENTIFY CONSISTENT P2P FIRMS                                  */
/* ============================================================================= */

/* Load dataset with aggregated industry data */
use "$output\ifp_scrapped_aggregated_industries_modified.dta", clear

/* Keep only firms where P2P lending was used (p2p == 1) */
drop if p2p == 0

/* Keep only firm identifiers */
keep siren 
sort siren

/* Remove duplicate siren values */
quietly by siren: gen dup = cond(_N==1,0,_n)
drop if dup > 1

/* Save cleaned list of P2P firms */
save "$output\p2p_1_siren_list.dta", replace

/* ============================================================================= */
/*        STEP 2: LOAD REJECTED FIRMS & MERGE WITH P2P FIRMS                     */
/* ============================================================================= */

use "$output\rejected_agg_industries_modified.dta", clear

/* Merge rejected firms with the list of P2P firms */
merge m:1 siren using "$output\p2p_1_siren_list.dta"

/* Keep firms that are either rejected (accepted == 0) or are in the P2P list */
keep if _merge == 3 | accepted == 0
drop _merge

/* Keep observations within [-12, 24] months from the event */
keep if inrange(deltamonths, -12, 24)

/* Generate log-transformed loan variable */
gen lloan = log(stloan + ltloan + bm + bi + oc)

/* Merge with test sample of rejected firms */
merge m:1 siren using "$output\rejected_ttest_siren_list.dta", keepusing(siren)
keep if _merge == 3

/* ============================================================================= */
/*        STEP 3: REGRESSIONS ON DIFFERENT MATCHING APPROACHES                    */
/* ============================================================================= */

/* ===== Unmatched sample regression ===== */
capture estimates drop *
qui eststo reg1: reghdfe lloan i1.accepted##i1.post, a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace

/* ===== Propensity Score Matching (PSM) - No Replacement ===== */
use "$output\matched_sample_ps_norep_rejected.dta", clear

/* Keep relevant date range */
keep if inrange(mdate, mofd(date("20140101","YMD")), mofd(date("20191231","YMD")))
keep if inrange(deltamonths, -12, 24)

/* Generate log-transformed loan variable */
gen lloan = log(stloan + ltloan + bm + bi + oc)

/* Regression with PSM (No Replacement) */
qui eststo reg2: reghdfe lloan i1.accepted##i1.post [pweight=weight], a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace

/* ===== Propensity Score Matching (PSM) - With Replacement ===== */
use "$output\matched_sample_ps_rep_industry_year_rejected.dta", clear

/* Keep relevant date range */
keep if inrange(mdate, mofd(date("20140101","YMD")), mofd(date("20191231","YMD")))
keep if inrange(deltamonths, -12, 24)

/* Generate log-transformed loan variable */
gen lloan = log(stloan + ltloan + bm + bi + oc)

/* Regression with PSM (With Replacement) */
qui eststo reg3: reghdfe lloan i1.accepted##i1.post [pweight=weight], a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace

/* ===== Nearest Neighbor Matching (NNM) ===== */
use "$output\rejected_psm.dta", clear

/* Keep relevant date range */
keep if inrange(mdate, mofd(date("20140101","YMD")), mofd(date("20191231","YMD")))
keep if inrange(deltamonths, -12, 24)

/* Generate log-transformed loan variable */
gen lloan = log(stloan + ltloan + bm + bi + oc)

/* Regression with Nearest Neighbor Matching */
qui eststo reg4: reghdfe lloan i1.accepted##i1.post [pweight=weight], a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace

/* ===== Firm Fixed Effects Regression (Alternative Specification) ===== */
qui eststo reg5: reghdfe lloan i1.accepted##i1.post [pweight=weight], a(i.siren mdate) cluster(siren) 
estadd local yearfirm "N", replace   
estadd local mdateFE "Y", replace  
estadd local firm "Y", replace
estadd local IRL "N", replace

/* ===== Excluding Outlying Platforms (Lendix & Lookandfin) ===== */
preserve
drop if outlyingplatform == 1
qui eststo reg7: reghdfe lloan i1.accepted##i1.post [pweight=weight], a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace
restore

/* ============================================================================= */
/*        STEP 4: EXPORT RESULTS TO LATEX TABLES                                 */
/* ============================================================================= */

/* Export first set of regressions */
esttab reg4 reg5 using "$tables/Table_4_acc_rej.tex", replace ///
	noomitted nobase fragment gap booktabs label nonote noobs nolines /// 
	b(%9.3f) drop(_cons) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) posthead(\midrule) ///
	mtitles("Unmatched" "5-nearest neighbor matching" "5-nearest neighbor matching" "PSM no rep." ///
	"PSM with rep." "Excl. Lendix & Lookandfin") ///
	order(1.accepted#1.post 1.post) varlabel(1.post "Post" 1.accepted#1.post `"FinTech $\times$ Post"' ) ///
	stats(yearfirm mdateFE firm IRL N r2, label(`"\midrule Firm-Year FE"' `"Month FE"' `"Firm FE"' `"IRL-Year FE"' `"\midrule N"' `"R-sq"') ///
	layout("\multicolumn{1}c{@}" "\multicolumn{1}c{@}" "\multicolumn{1}c{@}" "\multicolumn{1}c{@}") fmt(%9.0fc %9.0fc %9.0fc %9.0fc %9.0fc %9.2f ))

/* Export second set of regressions */
esttab reg1 reg2 reg3 reg7 using "$tables/Table_D1_Matching_Procedure_acc_rej.tex", replace ///
	noomitted nobase fragment gap booktabs label nonote noobs nolines /// 
	b(%9.3f) drop(_cons) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) posthead(\midrule) ///
	mtitles("Unmatched" "5-nearest neighbor matching" "5-nearest neighbor matching" "PSM no rep." ///
	"PSM with rep." "Excl. Lendix & Lookandfin") ///
	order(1.accepted#1.post 1.post) varlabel(1.post "Post" 1.accepted#1.post `"FinTech $\times$ Post"' ) ///
	stats(yearfirm mdateFE firm IRL N r2, label(`"\midrule Firm-Year FE"' `"Month FE"' `"Firm FE"' `"IRL-Year FE"' `"\midrule N"' `"R-sq"') ///
	layout("\multicolumn{1}c{@}" "\multicolumn{1}c{@}" "\multicolumn{1}c{@}" "\multicolumn{1}c{@}") fmt(%9.0fc %9.0fc %9.0fc %9.0fc %9.0fc %9.2f ))

/* Close log file */
log close
